<?php
require_once('./config.inc');
require_once('./variables.inc');
require_once('./libccramp.inc');

//session_register($U_id);
$U_id = $_SESSION[U_id];

switch($_REQUEST['do']){

case 'cmjTsvExport':
   ?><?php 
   $q = explode('::', $_REQUEST[q]);
   for ($i=0; $i < count($q); $i++) {
      $qTemp = explode('|', $q[$i]);
      $cmj[$i][albId] = $qTemp[0];
      $cmj[$i][spins] = $qTemp[1];   
   }
   $cmjCsvContents = '';
   for ($i=0; count($cmj) > $i; $i++) {
      $albSql = " SELECT * FROM `album` WHERE (`cddbid`='".$cmj[$i][albId]."') LIMIT 1 ";
      $albQuery = mysql_query($albSql);
      $alb = mysql_fetch_array($albQuery);
      $cmjCsvContents .= ($i+1) .'	'."$alb[artist]".'	'."$alb[title]".'	'."$alb[label]"."\n";
   }
//   var_dump($cmj);
//   echo('cmjListTbody||'.$cmjCsvContents);
   fwrite(fopen('../OUTGOING/cmj.csv', 'w'), $cmjCsvContents );
   break;
   
case 'cmjMvAlbUp':
   ?>cmjListTbody||<?php 
   $q = explode('::', $_REQUEST[q]);
   $r = $_REQUEST[r];
   
   for ($i=0; $i < count($q); $i++) {
      $qTemp = explode('|', $q[$i]);
      $cmj[$i][albId] = $qTemp[0];
      $cmj[$i][spins] = $qTemp[1];   
   }
   
   $cmjDownTemp[albId] = $cmj[($r-2)][albId];
   $cmjDownTemp[spins] = $cmj[($r-2)][spins];
   $cmj[($r-2)][albId] = $cmj[($r-1)][albId];
   $cmj[($r-2)][spins] = $cmj[($r-1)][spins];
   $cmj[($r-1)][albId] = $cmjDownTemp[albId];
   $cmj[($r-1)][spins] = $cmjDownTemp[spins];
   
   for ($i=0; $i < (count($cmj)-1); $i++) {
      $row = $i+1;      
      $albQuery = mysql_query("SELECT `artist`,`title` FROM `album` WHERE `cddbid`='".$cmj[$i][albId]."' LIMIT 1 ");
      $alb = mysql_fetch_array($albQuery);
      ?>
      <tr>
         <td><?php 
         if ($i != 0) {
            ?><a onClick="cmjMvAlbUp(<?php echo($row) ?>);"><img src="./themes/<?=$theme?>/img/up.png" alt="arrow, up" title="move up" /></a><?php 
         }
         ?>
            <input type="hidden" name="cmj[<?php echo($row) ?>][albId]" id="cmj[<?php echo($row) ?>][albId]" value="<?php echo($cmj[$i][albId]) ?>" />
            <input type="hidden" name="cmj[<?php echo($row) ?>][spins]" id="cmj[<?php echo($row) ?>][spins]" value="<?php echo($cmj[$i][spins]) ?>" />
         </td>
         <td><?php echo($row) ?></td>
         <td><?php echo($cmj[$i][spins]) ?></td>
         <td><?php echo($alb[artist]) ?> (<?php echo($alb[title]) ?>)</td>
         </tr>
      <?php 
//      $albQuery = mysql_query("SELECT `artist` FROM `album` WHERE `cddbid`='".$cmj[$i][albId]."' LIMIT 1 ");
//      $alb = mysql_fetch_array($albQuery);
//      echo( ($i+1).' '.$cmj[($i)][spins].' '.$alb[artist].'<br />');
   } 
   ?>
         <tr><td></td><td colspan="3"><a onClick="cmjTsvExport();" href="./OUTGOING/cmj.csv">export as tsv</a></td></tr>
   <?php 
   break;
   

// 
// this outputs the users's playlist so they can easily copy it to remember it or post it on their blog or whatever
// 
case 'showAirExport':

   $outputType = $_REQUEST[outputType];
   if ($outputType == 'hide') {
      ?>airExport|| <?php
   } else {
      ?>airExport||<a onClick="showAirExport('hide'); " class="helpInfo">(hide)</a><br /><a onClick="showAirExport('text');">plain text</a> | <a onClick="showAirExport('html');">html</a>
      <br />
      <?php
      
      $airSql = " SELECT * FROM `airplay` WHERE (`timestamp`>'".$three_hours_ago."') AND (`user`='".$U_id."') ";
      $airQuery = mysql_query($airSql);
      ?><textarea style="height: 250px; width: 550px;"><?php 
      if (!$outputType || $outputType === 'text') {
         while ($air = mysql_fetch_array($airQuery)) {
            printf('%s: "%s" [from %s]%s', $air[artist], $air[track], $air[album], "\n");
         }
      } else if ($outputType === 'html') {
         ?><?php 
         while ($air = mysql_fetch_array($airQuery)) {
            printf('&lt;b&gt;%s:&lt;/b&gt; &lt;i&gt;%s&lt;/i&gt; [from %s]&lt;br /&gt;%s', $air[artist], $air[track], $air[album], "\n");
         }
         ?><?php 
      }
      ?></textarea><?php 
   }
   break;
   
case 'unsetRequest':
   if (mysql_query("UPDATE `airplay` SET `request`='0' WHERE `id`='".$_REQUEST['id']."' ")) {
		$trackId = mysql_fetch_array(mysql_query("SELECT `trackno`,`cddbid` FROM `airplay` WHERE `id`='".$_REQUEST['id']."' "));
		$trackTitle = mysql_fetch_array(mysql_query("SELECT `title` FROM `track` WHERE (`trackno`='".$trackId['trackno']."') AND (`cddbid`='".$trackId['cddbid']."') "));
		echo('alertMessage||"' . trunc($trackTitle[0], 25) . '" has been set as a non-request.');
	} else {
		echo('alertMessage||There was an error; the track you selected was not set as a non-request. mysql said: '.mysql_error());
	}
	break;
	
case 'setRequest':   
	if (mysql_query("UPDATE `airplay` SET `request`='1' WHERE `id`='".$_REQUEST['id']."' ")) {
		$trackId = mysql_fetch_array(mysql_query("SELECT `trackno`,`cddbid` FROM `airplay` WHERE `id`='".$_REQUEST['id']."' "));
		$trackTitle = mysql_fetch_array(mysql_query("SELECT `title` FROM `track` WHERE (`trackno`='".$trackId['trackno']."') AND (`cddbid`='".$trackId['cddbid']."') "));
		echo('alertMessage||"' . trunc($trackTitle[0], 25) . '" has been set as a request.');
	} else {
		echo('alertMessage||There was an error; the track you selected was not set as a request.');
	}
	break;
	
case 'delAlbum':
	$albId = $_REQUEST['albId'];
   $albQuery = mysql_query("SELECT `title` FROM `album` WHERE `cddbid` = '".$albId."' LIMIT 1");
   $alb = mysql_fetch_array($albQuery);
   if (mysql_query("DELETE FROM `album` WHERE `cddbid` = '".$albId."' ")) {
   	   $tracksDeleted = mysql_result(mysql_query("SELECT COUNT(*) FROM `track` WHERE `cddbid` = '$albId' "), 0);
   	   mysql_query("DELETE FROM `track` WHERE `cddbid`='".$albId."' ");
   	   echo('alertMessage||The album "'.$alb['title'].'" and '.$tracksDeleted.' tracks have been deleted from the database, but files may still exist.');
   } else {
      echo('alertMessage||Sorry, that album could not be deleted.');
   }
   break;
   
case 'pickAlb':
//   echo('alertMessage||test');
   $albId = $_REQUEST['albId'];
   $usrId = $_REQUEST['usrId'];
   mysql_query("UPDATE `users` SET `pick` = '".$albId."' WHERE `id` = '".$usrId."' ");
   $pickCheckQuery = mysql_query("SELECT `title` FROM `album` WHERE (`cddbid`='".$albId."') LIMIT 1" );
   $pickCheck = mysql_fetch_array($pickCheckQuery);
   printf('alertMessage||The album "%s" is now your current pick!', $pickCheck[title]);
   break;
   
case 'expandAir':
   $airId = $_REQUEST['airId'];
   $rowNum = $_REQUEST['rowNum'];
   $airQuery = mysql_query("SELECT * FROM `airplay` WHERE (`id`='".$airId."') ");
   $air = mysql_fetch_array($airQuery);
   echo('air_trunc['.$rowNum.']||');
   ?>
   <td><?php echo($air[slot]) ?></td>
   <td><?php echo($air[trackno]) ?></td>
   <td><a onClick="collapseAir(<?php echo($rowNum.', '.$airId) ?>)">+</a></td>
   <td colspan="3">
   Title: <?php echo($air[track]) ?><br />
   Artist: <?php echo($air[artist]) ?><br />
   Album: <?php echo($air[album]) ?><br />
<a title="Click this icon to select this album as your pick!" onClick="pickAlb(<?php echo($air[cddbid].', '.$U_id) ?>)" ><img src="./themes/<?=$theme?>/img/pick.png" width="15" height="15" border="0" alt="pick"></a>
<a target="_new" href="http://bandtoband.com/index.php?Page=Search&Keywords=<?=htmlspecialchars($air['artist'])?>"><img style="border: none;" src="themes/<?=$theme?>/img/b2b.png" height="16" width="16" /></a>
<a target="_new" href="http://www.last.fm/music/<?=htmlspecialchars(str_replace(' ', '+', $air['artist']))?>"><img style="border: none;" src="themes/<?=$theme?>/img/lastfm.gif" height="16" width="16" /></a>
<a target="_new" href="http://www.musicbrainz.org/newsearch.html?table=artist&search=<?=htmlspecialchars($air['artist'])?>"><img style="border: none;" src="themes/<?=$theme?>/img/musicbrainz.png" height="16" width="16" /></a>
<a target="_new" href="http://www.allmusic.com/cg/amg.dll?p=amg&sql=1:<?=htmlspecialchars(str_replace(' ', '|', $air['artist']))?>"><img style="border: none;" src="themes/<?=$theme?>/img/allmusic.png" height="16" width="16" /></a>
<a target="_new" href="http://en.wikipedia.org/wiki/<?=str_replace(' ', '_', htmlspecialchars($air['artist']))?>"><img style="border: none;" src="themes/<?=$theme?>/img/wikipedia.png" height="16" width="16" /></a>
<a target="_new" href="http://www.pitchforkmedia.com/cgi-bin/search2/search.cgi?terms=<?=htmlspecialchars($airplayrow['artist'])?>"><img style="border: none;" src="themes/<?=$theme?>/img/pitchfork.png" height="16" width="16" /></a>
   </td>
   <td><?php if($air[status] < 3) echo('new') ?></td>
   <td><?php if ($air[status] >=3) echo('old') ?></td>
   <td><?php if ($air[request]) echo('!!') ?></td>
   <td><span style="display: inline !important;"><a href="?mode=traffic&do=air_down&timestamp=<?=$air[timestamp]?>" title="Move this log item down"><img style="border: 0px;" height="16" width="16" title="Move this log item down one space" alt="arrow, move down" src="./themes/<?=$theme?>/img/down.png" /></a><a href="?mode=traffic&do=air_edit&timestamp=<?=$air[timestamp]?>" title="Edit this log item"><img style="border: 0px;" height="16" width="16" title="edit this log item" alt="page, edit icon" src="./themes/<?=$theme?>/img/edit.png"  /></a><a href="?mode=traffic&do=air_delete&timestamp=<?=$air[timestamp]?>" title="delete this song from the log" onclick="return confirm('Do you really want to delete this log entry?')"><img style="border: 0px;" height="16" width="16" title="delete this entry" alt="trash, delete" src="./themes/<?=$theme?>/img/trash.png" /></a></span></td>
   <?php 
   break;
   
case 'collapseAir':            
   $airId = $_REQUEST['airId'];
   $rowNum = $_REQUEST['rowNum'];                                                        
   $airQuery = mysql_query("SELECT * FROM `airplay` WHERE (`id`='".$airId."') ");
   $air = mysql_fetch_array($airQuery);
   echo('air_trunc['.$rowNum.']||');
   ?>
   <td><?php echo($air[slot]) ?></td>
   <td><?php echo($air[trackno]) ?></td>
   <td><a onClick="expandAir(<?php echo($rowNum) ?>, <?php echo($airId) ?>)">+</a></td>
   <td><?php echo($air[track]) ?></td>
   <td><?php echo($air[artist]) ?></td>
   <td><?php echo($air[album]) ?></td>
   <td>new</td>
   <td>old</td>
   <td>req</td>
   <td><a href="?mode=traffic&do=air_down&timestamp=<?=$air[timestamp]?>" 
title="Move this log item down"><img style="border: 0px;" height="16" width="16" title="Move this log item down one space" 
alt="arrow, move down" src="./themes/<?=$theme?>/img/down.png" /></a><a href="?mode=traffic&do=air_edit&timestamp=<?=$air[timestamp]?>" 
title="Edit this log item"><img style="border: 0px;" height="16" width="16" title="edit this log item" alt="page, edit icon" src="./themes/<?=$theme?>/img/edit.png" 
 /></a><a href="?mode=traffic&do=air_delete&timestamp=<?=$air[timestamp]?>" 
title="delete this song from the log" onclick="return confirm('Do you really want to delete this log entry?')"><img 
style="border: 0px;" height="16" width="16" title="delete this entry" alt="trash, delete" src="./themes/<?=$theme?>/img/trash.png" /></a></td>
   <?php
   break;
   
case 'updateTrafLogger':
   
   if ($_REQUEST['schedId']) $schedId = $_REQUEST['schedId'];
   
   echo('trafLogger||');
   
   $now = time(); // get the current time to ensure consistency of time in setting variables
   //echo('<h1>'.$now.'</h1>');
//   $now = strtotime(date('Y-m-d H:i:s', mktime( date('H', $now) + 6, date('i', $now), date('s', $now), date('m', $now), date('d', $now), date('Y', $now) ) ) );
   
   $trafHistMinOffset = 75;
   $trafHistMin = date('Y-m-d H:i:s', mktime(date('H', $now), date('i', $now) - $trafHistMinOffset, date('s', $now), date('m', $now), date('d', $now), date('Y', $now)));
   $trafHistQuery = mysql_query("SELECT `sched_id` FROM `traffic` WHERE `timestamp`>'".$trafHistMin."' ");
   $i = 0;
   
   $hist = array();
   while ($trafHist = mysql_fetch_array($trafHistQuery)) {
      $hist[$i++] = $trafHist[sched_id];
   }
   unset($i);

   // how many minutes should we go back when searching for traffic events?  the default is 26 minutes prior to the current time.
   $trafTimeMinOffset = 26;
   $trafTimeMkMin = mktime(date('H', $now), date('i', $now) - $trafTimeMinOffset, date('s', $now), date('m', $now), date('d', $now), date('Y', $now));
   // this will be the minimum time in the SQL query 
   $trafTimeMin = date('H:i:s', $trafTimeMkMin);
   
   $trafTimeMinDay = date('N', $trafTimeMkMin); // we need to know the day, in case we've just passed midnight and the minimum flows into the previous day 
   
   // how many minutes forward should we show upcoming traffic events?  the default is 61 minutes from current time.
   $trafTimeMaxOffset = 61;
   $trafTimeMkMax = mktime(date('H', $now), date('i', $now) + $trafTimeMaxOffset, date('s', $now), date('m', $now), date('d', $now), date('Y', $now));
   $trafTimeMax = date('H:i:s', $trafTimeMkMax);
   $trafTimeMaxDay = date('N', $trafTimeMkMax); // incase it's just prior to midnight and we need tomorrow's day
   
   $trafTimeNow = date('H:i:s', $now); // current time 
   
   $trafUrgentMin = 3; // how many minutes in the past before we make a traffic event display as urgent?
   $trafWarnMin = 5; // how many minutes into the future before we make a traffic event display as a warning?
   
   // current day of the week, used to check the schedule for daily things 
   $today = date('N', $now);
   $todayYMD = date('Y-m-d', $now);
   if ($today == $trafTimeMaxDay && $today == $trafTimeMinDay) {
      $getTrafQuery = mysql_query("SELECT * FROM `schedule` 
      WHERE (`weekday`='".$today."') 
      AND (`starttime` >= '".$trafTimeMin."') 
      AND (`starttime` <= '".$trafTimeMax."') 
      ORDER BY `starttime` ASC ");
      $i = 0;
      while ($trafTemp = mysql_fetch_array($getTrafQuery)) {
         if (!$hist || !in_array($trafTemp[id], $hist)) $traf[$i++] = $trafTemp; 
      }
   } else if ($today !== $trafTimeMaxDay) { 
      $getTrafQuery = mysql_query("SELECT * FROM `schedule` 
      WHERE ((`weekday`='".$today."') AND (`starttime` >= '".$trafTimeMin."' )) 
      ORDER BY `starttime` ASC ");
      $i = 0;
      while ($trafTemp = mysql_fetch_array($getTrafQuery)) {
         if (!in_array($trafTemp[id], $hist)) $traf[$i++] = $trafTemp;
      }
      $getTrafQuery = mysql_query("SELECT * FROM `schedule` 
      WHERE ((`weekday` = '".$trafTimeMaxDay."') AND (`starttime` <= '".$trafTimeMax."')) 
      ORDER BY `starttime` ASC ");
      while ($trafTemp = mysql_fetch_array($getTrafQuery)) {
         if (!in_array($trafTemp[id], $hist)) $traf[$i++] = $trafTemp;
      }
   } else {
      $getTrafQuery = mysql_query("SELECT * FROM `schedule` 
      WHERE ((`weekday`='".$trafTimeMinDay."' ) AND (`starttime` >= '$trafTimeMin')) 
      ORDER BY `starttime` ASC ");
      $i = 0;
      while ($trafTemp = mysql_fetch_array($getTrafQuery)) {
         if (!in_array($trafTemp[id], $hist)) $traf[$i++] = $trafTemp;
      }
      $getTrafQuery = mysql_query("SELECT * FROM `schedule` 
      WHERE ((`weekday`='".$today."') AND (`starttime`<='".$trafTimeMax."'))
      ORDER BY `starttime` ASC ");
      while ($trafTemp = mysql_fetch_array ($getTrafQuery)) {
         if (!in_array($trafTemp[id], $hist)) $traf[$i++] = $trafTemp;
      }
   }
//   $getTrafQuery = mysql_query($getTrafSql);
   ?>
   <form name="trafAdd" action="?mode=traffic" method="post">
   <table><?php 
   $trafRows = count($traf);
   if ($traf) {
   foreach ($traf as $traf) {
      ?><tr><?php
      
      // get the name/eventtype from the event table in the database 
      $eventName = mysql_result(mysql_query("SELECT `name` FROM `event` WHERE `id`='".$traf[eventtype]."' LIMIT 1 "), 0);
      
      // format the time in a more human-readable form
      $trafTime = date('g:i', strtotime(($todayYMD.' '.$traf[starttime])));
      
      if (!$schedId) $schedId = $traf[id];

      // Not sure about this line, but we need some way to transmit data to the form. 
      if ($schedId == $traf[id]) $trafAdd = $traf;
      
      ?>
      <td <?php 
      if ($schedId == $traf[id]) echo('class="trafSelected"');
      else echo('class="trafNormal"');
      echo('>'); 
      
      if ( ($traf[starttime] < $trafTimeNow) && ($traf[day] === $trafTimeMinDay) ) echo ('<span class="trafUrgent">'.$trafTime.'</span>');
	   else echo ('<span class="trafNearFuture">'.$trafTime.'</span>');
	   echo (' <span class="trafText" onClick="updateTrafLogger('.$traf[id].'); haltTrafLogger();">'.$eventName.'</span> <br />');
	   ?></td>
	   <?php 
	   if (!$colTrigger) {
	      echo('
	      <td style="border-right: none;" class="trafComment" rowspan="'.$trafRows.'"> Comments:<br />
	      <textarea name="comment" onClick="haltTrafLogger()" onInput="haltTrafLogger()"></textarea>
	      </td><td rowspan="'.$trafRows.'" class="trafComment"><a onClick="trafDismiss('.$schedId.');">dismiss</a> <input type="submit" value="submit" /></td>');
	      $colTrigger = true;
	   }
	   ?>
	   </tr><?php
	   // alert(trafLoggerInterval); clearInterval(trafLoggerInterval); alert(\'stopped\'); 
   } // end foreach ($traf as traf)
   } // end if($traf)
   ?></table>
   <input type="hidden" name="eventtype" value="<?=$trafAdd[eventtype]?>" />
	<input type="hidden" name="sched_id" value="<?=$trafAdd[id]?>" />
	<input type="hidden" name="mode" value="traffic" />
	<input type="hidden" name="do" value="add_event_to_traffic" />
	</form>
   <?php 
   break;
   
case 'trafDismiss':
   $trafId = $_REQUEST['trafId'];
   echo('alertMessage||');
   $dismissQuery = mysql_query("INSERT INTO `traffic` SET `sched_id`='".$trafId."',`timestamp`='".date('Y-m-d H:i:s')."',`dummy`='1' ");
   if ($dismissQuery) {
      echo('event successfully dismissed');
   } else {
      echo('there was a problem dismissing the event.  Mysql said: '.mysql_error());
   }
   ?><img src="./themes/<?=$theme?>/img/blank.png" onload="updateTrafLogger();" /><?php
   break;

case 'rmTrk':
   $trkId = $_REQUEST['trkId'];
   if (mysql_query("DELETE FROM `track` WHERE (`id` = '".$trkId."') ")) {
      echo('alertMessage||that track was successfully deleted from the database.');
   } else {
      echo('alertMessage||there was a problem deleting that track.  Mysql returned error:' . mysql_error());
   }
   break;

/*
 * SECTION albEdit
 * 
 * these "ajax" quasi-functions belong to the albEdit mode, primarily or exclusively.
 *
*/

case 'rateAlb':
   $albVirtue = $_REQUEST['albVirtue'];
   if (!$albVirtue) $albVirtue = 3;
   echo('albVirtue||<input type="hidden" name="albVirtue" value="'.$albVirtue.'" />');
   
   if ($albVirtue == 1) {
      echo('
      <img src="./themes/'.$theme.'/img/virtue1_on.png" onClick="rateAlb(1)" /><img src="./themes/'.$theme.'/img/virtue2_off.png" onClick="rateAlb(2)" /><img src="./themes/'.$theme.'/img/virtue3_off.png" onClick="rateAlb(3)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(4)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(5)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(6)" />');
   } else if ($albVirtue == 2) {
      echo('
      <img src="./themes/'.$theme.'/img/virtue1_off.png" onClick="rateAlb(1)" /><img src="./themes/'.$theme.'/img/virtue2_on.png" onClick="rateAlb(2)" /><img src="./themes/'.$theme.'/img/virtue3_off.png" onClick="rateAlb(3)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(4)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(5)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(6)" />');
   } else if ($albVirtue == 3) {
      echo('
      <img src="./themes/'.$theme.'/img/virtue1_off.png" onClick="rateAlb(1)" /><img src="./themes/'.$theme.'/img/virtue2_off.png" onClick="rateAlb(2)" /><img src="./themes/'.$theme.'/img/virtue3_on.png" onClick="rateAlb(3)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(4)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(5)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(6)" />');  
   } else if ($albVirtue == 4) {
      echo('
      <img src="./themes/'.$theme.'/img/virtue1_off.png" onClick="rateAlb(1)" /><img src="./themes/'.$theme.'/img/virtue2_off.png" onClick="rateAlb(2)" /><img src="./themes/'.$theme.'/img/virtue3_off.png" onClick="rateAlb(3)" /><img src="./themes/'.$theme.'/img/star_selected.png" onClick="rateAlb(4)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(5)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(6)" />');
   } else if ($albVirtue == 5) {
      echo('
      <img src="./themes/'.$theme.'/img/virtue1_off.png" onClick="rateAlb(1)" /><img src="./themes/'.$theme.'/img/virtue2_off.png" onClick="rateAlb(2)" /><img src="./themes/'.$theme.'/img/virtue3_off.png" onClick="rateAlb(3)" /><img src="./themes/'.$theme.'/img/star_selected.png" onClick="rateAlb(4)" /><img src="./themes/'.$theme.'/img/star_selected.png" onClick="rateAlb(5)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(6)" />');
   } else if ($albVirtue > 5) {
      echo('
      <img src="./themes/'.$theme.'/img/virtue1_off.png" onClick="rateAlb(1)" /><img src="./themes/'.$theme.'/img/virtue2_off.png" onClick="rateAlb(2)" /><img src="./themes/'.$theme.'/img/virtue3_off.png" onClick="rateAlb(3)" /><img src="./themes/'.$theme.'/img/star_selected.png" onClick="rateAlb(4)" /><img src="./themes/'.$theme.'/img/star_selected.png" onClick="rateAlb(5)" /><img src="./themes/'.$theme.'/img/star_selected.png" onClick="rateAlb(6)" />');
   } else {
      echo('
      <img src="./themes/'.$theme.'/img/virtue1_off.png" onClick="rateAlb(1)" /><img src="./themes/'.$theme.'/img/virtue2_off.png" onClick="rateAlb(2)" /><img src="./themes/'.$theme.'/img/virtue3_off.png" onClick="rateAlb(3)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(4)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(5)" /><img src="./themes/'.$theme.'/img/star_unselected.png" onClick="rateAlb(6)" />
      ');
   }
   break;
   
//case 'rateTrk':
//   $trkRow = $_REQUEST['trkRow'];
//   $trkVirtue = $_REQUEST['trkVirtue'];
//   echo('rateTrk['.$trkRow.']||test');
//   break;

case 'addTrk':
   $trkRow = $_REQUEST['trkRow'];
   $isComp = $_REQUEST['isComp'];
   //$extInfo = $_REQUEST['extInfo'];
   $albId = $_REQUEST['albId'];
   
   ?>addTrkTrigger||<a onClick="addTrk(<?=($trkRow + 1)?>)">add new track</a><input type="text" name="lastTrk" id="lastTrk" value="<?=$trkRow?>" />
   <?php 
   
   echo('||trkRow['.$trkRow.']||');
   ?><td></td><td class="tracklist">
   <input type="hidden" name="trk[<?=$trkRow?>][id]" value="new" />
   <img src="./themes/<?=$theme?>/img/blank.png" style="height: 1px; width: 1px;" onLoad="document.getElementById('trkTitle<?=$trkRow?>').focus()">
   <?=$trkRow?><input type="hidden" name="trk[<?=$trkRow?>][num]" value="<?=$trkRow?>" /></td>
   <td class="tracklist"><input onBlur="blurAddTrk(<?=$trkRow?>);" type="text" name="trk[<?=$trkRow?>][title]" id="trkTitle<?=$trkRow?>" tabindex="<?echo(16 + $trkRow)?>" />
	</td><td class="tracklist"><span id="rateTrk[<?=$trkRow?>]">
	<img src="./themes/<?=$theme?>/img/virtue1_off.png" onClick="rateTrk(<?=$trkRow?>,1)" /><img src="./themes/<?=$theme?>/img/virtue2_off.png" onClick="rateTrk(<?=$trkRow?>,2)" /><img src="./themes/<?=$theme?>/img/virtue3_off.png" onClick="rateTrk(<?=$trkRow?>,3)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(<?=$trkRow?>,4)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(<?=$trkRow?>,5)" /><img src="./themes/<?=$theme?>/img/star_unselected.png" onClick="rateTrk(<?=$trkRow?>,6)" /></span></td>
	<td class="tracklist"><span class="trkArtist<?php if (!$isComp) echo('Hidden')?>"><input  onBlur="blurAddTrk(<?=$trkRow?>)" id="trkArtist<?=$trkRow?>" name="trk[<?=$trkRow?>][artist]" type="text" /></span>
	</td>
	||trkExtInfo[<?=$trkRow?>]||<td> </td><td> </td><td>extended info</td><td></td><td></td>
	<?php
   break;
   
case 'albSetMulti':
   $albId = $_REQUEST['albId'];
   echo('albMulti||');
   $alb = mysql_fetch_array(mysql_query("SELECT `multi_id`,`cddbid`,`multi` FROM `album` WHERE (`cddbid`='".$albId."') LIMIT 1 "));
//   if ($multiId) {
//      
//   }
   echo('<span class="inputTitleLeft"> Alt. ID#</span><input name="multi_id" value="'.$alb[multi_id].'" type="text" style="width: 50px;" name="multi_id" onClick="parent.albSetMultiQuery(\''.$alb[cddbid].'\', \''.$alb[multi].'\', event.x, event.y)" />');
   if (!$alb[multi_id]) echo('<input type="button" value="set" />');
   break;
//   $albId = $_REQUEST['albId'];
//   echo('albMulti||');
//   echo('<span class="inputTitleLeft"> Alt. ID#</span><input type="text" style="width: 50px;" name="multi_id" onMouseOver="parent.testAlert(); albSetMultiQuery(\''.$albId.'\')" />');
//   break;



/*
 * SECTION mkPlaylist
 * 
 * These ajax quasi-functions belong primarily or exclusively to the mkPlaylist and mkManPlaylist modes
 *
*/

case 'randPlayTrk':
   $randAlbQuery = mysql_query("SELECT `cddbid` FROM `album` WHERE (`status`<'3') ORDER BY RAND() LIMIT 1 ");
   $randTrkQuery = mysql_query("SELECT * FROM `track` WHERE (`cddbid`='".mysql_result($randAlbQuery,0)."') AND (`virtue`>'2') ORDER BY RAND() LIMIT 1");
   $trk = mysql_fetch_array($randTrkQuery);
//   $trkId = mysql_result(mysql_query("SELECT `id` FROM `track` WHERE `cddbid`='".mysql_result(mysql_query('SELECT `cddbid` FROM `album` ORDER BY RAND() LIMIT 1'), 0)."' ORDER BY RAND() LIMIT 1 "), 0);
   echo ('playItem['.$_REQUEST['playRow'].']||
   '.$trk[title].' by '.$trk[artist].'</span>
   <br />
   <span id="playItem['.($_REQUEST['playRow']+1).']">
   <div onClick="randPlayTrk('.($_REQUEST[playRow]+1).')" ><img src="./themes/'.$theme.'/img/dice.png" alt="dice" title="add random track" /> DICE!!!</div>');
   break;

case 'togglePlayChunk':
   $chunkStatus = $_REQUEST[chunkStatus];
   $playId = $_REQUEST[playId];
   echo('playChunkStatus['.$playId.']||
   <input type="hidden" name="play['.$playId.'][chunk]" value="'.$chunkStatus.'" />');
   if ($chunkStatus == 1) {
      echo('<img src="./themes/'.$theme.'/img/blank.png" style="height: 11px; width: 11px;" onClick="togglePlayChunk('.$playId.',2)" />');
   } else if ($chunkStatus == 2) {
      echo ('<img src="./themes/'.$theme.'/img/bracket-top-11x11.png" onClick="togglePlayChunk('.$playId.',3)" />');
   } else if ($chunkStatus == 3) {
      echo ('<img src="./themes/'.$theme.'/img/bracket-middle-11x11.png" onClick="togglePlayChunk('.$playId.',4)" />');
   } else if ($chunkStatus == 4) {
      echo ('<img src="./themes/'.$theme.'/img/bracket-bottom-11x11.png" onClick="togglePlayChunk('.$playId.',1)" />');
   }
   break;

case 'test':
   echo('ajaxtest||test');
   break;

}
?>
